package com.adwhirl.adapters;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlTargeting;
import com.adwhirl.AdWhirlTargeting.Gender;
import com.adwhirl.obj.Ration;
import com.lifestreet.android.ad.adview.AdListener;
import com.lifestreet.android.ad.adview.AdRequest;
import com.lifestreet.android.ad.adview.AdSize;
import com.lifestreet.android.ad.adview.AdView;
import com.lifestreet.android.ad.common.MyLog;

public class LifeStreetAdapter extends AdWhirlAdapter implements AdListener {

	public LifeStreetAdapter(AdWhirlLayout adWhirlLayout, Ration ration) {
		super(adWhirlLayout, ration);
	}

	@Override
	public void handle() {
	    AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();
	    if (adWhirlLayout == null) {
	    	MyLog.error("adWhirlLayout is NULL");
	    	return;
	    }	    
	    Activity activity = adWhirlLayout.activityReference.get();
	    if (activity == null) {
	    	MyLog.error("activity is NULL");
	    	return;
	    }

	    String slotURL = null;
		try {
			JSONObject jsonObject = (JSONObject) new JSONTokener(ration.key).nextValue();
			slotURL = jsonObject.getString("SlotURL");  
		} catch (JSONException e) {
			MyLog.error("JSON Configuration exception", e);
			return;
		}
		if (slotURL == null) {
	    	MyLog.error("SlotURL is null");
	    	return;			
		}		
	    
		// INIT Request
		AdRequest adRequest = new AdRequest(activity, slotURL);

		// Set package name
		adRequest.setPackageId(activity.getPackageName());

		// Set DID
		try {
			final TelephonyManager tm = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);		
			adRequest.setDeviceId(tm.getDeviceId());			
		} catch (Exception e) {
			MyLog.warning("Couldn't get the device Id");
		}

		// Set birth date
		if (AdWhirlTargeting.getBirthDate() != null) {
			adRequest.setDateOfBirth(AdWhirlTargeting.getBirthDate().getTime().toString());	
		}
		
		// Set Gender
		if (!Gender.UNKNOWN.equals(AdWhirlTargeting.getGender())) {
			if (Gender.MALE.equals(AdWhirlTargeting.getGender())) {
				adRequest.setGender("male");
			} else {
				adRequest.setGender("female");
			}				
		}
		
		// Set Keywords
		if (AdWhirlTargeting.getKeywords() != null) {
			adRequest.setKeywords(AdWhirlTargeting.getKeywords());
		}
		
		// Set Zip code
		if (AdWhirlTargeting.getPostalCode() != null) {
			adRequest.setZip(AdWhirlTargeting.getPostalCode());
		}

	    // INIT AdView
		AdView lsmAdView = new AdView(activity, AdSize.BANNER_320x50, adRequest, this);		
		lsmAdView.loadAd();		
	}

	@Override
	public void didReveiveAd(AdView adView) {
		MyLog.info("didReveiveAd event");
	    AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();
	    if (adWhirlLayout == null) {
	    	MyLog.error("adWhirlLayout is NULL");
	    	return;
	    }
		adWhirlLayout.adWhirlManager.resetRollover();
		adWhirlLayout.handler.post(new AdWhirlLayout.ViewAdRunnable(adWhirlLayout, adView));
		adWhirlLayout.rotateThreadedDelayed();				
	}

	@Override
	public void didFailToReceiveAd() {
		MyLog.error("didFailToReceiveAd");
	    AdWhirlLayout adWhirlLayout = adWhirlLayoutReference.get();
	    if (adWhirlLayout == null) {
	    	MyLog.error("adWhirlLayout is NULL");
	    	return;
	    }
	    adWhirlLayout.rollover();
	}

	@Override
	public void didReveiveRefreshAd(AdView adView) {
		MyLog.info("didReveiveRefreshAd");
	}

	@Override
	public void didFailToReceiveRefreshAd() {
		MyLog.info("didFailToReceiveRefreshAd");
	}

	@Override
	public void applicationWillTerminateFromAd() {
		MyLog.info("applicationWillTerminateFromAd");
	}

}
